using System;
using System.Threading.Tasks;
using UnityEngine;
public static class SetRoomIdSocket
{
    
    public static async Task SendSetRoomIdAsync(long roomId)
    {
        try
        {
            var data = new RoomIdData { room_id = roomId };
            Debug.Log($"准备发送设置房间ID的命令: {JsonUtility.ToJson(data)}");
            await SendCommandAsync("setRoomId", data, GenerateMsgId());
        }
        catch (Exception ex)
        {
            Debug.LogError($"发送房间ID失败: {ex.Message}");
        }
    }

    private static long GenerateMsgId()
    {
        return DateTime.Now.Ticks % 1000000;
    }

    // 新增发送结构化消息的方法
    public static async Task SendCommandAsync(string command, RoomIdData roomData, long msgId)
    {
        // 使用可序列化的类替代匿名类型
        var message = new SetRoomIDMessage
        {
            cmd = command,
            data = roomData,
            timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds(),
            msg_id = msgId
        };
        
        Debug.Log($"准备发送命令: {command}, 数据: {JsonUtility.ToJson(roomData)}, 消息ID: {msgId}");
        string json = JsonUtility.ToJson(message);
        Debug.Log($"准备发送结构化命令: {json}");
        await WebSocketManager.Instance.SendMessageAsync(json);
    }
}

[System.Serializable]
public class RoomIdData
{
    public long room_id;
}
[System.Serializable]
public class SetRoomIDMessage
{
    public string cmd;
    public RoomIdData data;
    public long timestamp;
    public long msg_id;
}

